package com.yangjiayu.controller;

import com.tencentcloudapi.mrs.v20200910.models.BaseInfo;
import com.yangjiayu.base.BaseInfoProperties;
import com.yangjiayu.grace.enums.YesOrNo;
import com.yangjiayu.grace.result.GraceJSONResult;
import com.yangjiayu.grace.result.ResponseStatusEnum;
import com.yangjiayu.grace.utils.PagedGridResult;
import com.yangjiayu.pojo.bo.InterviewerBO;
import com.yangjiayu.pojo.bo.QuestionLibBO;
import com.yangjiayu.pojo.vo.InitQuestionsVO;
import com.yangjiayu.service.QuestionLibService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.yangjiayu.base.BaseInfoProperties.REDIS_USER_INFO;

/**
 * @Classname QuestionLibController
 * @Description TODO
 * @Date 2025/6/19 15:12
 * @Created by YangJiaYu
 */
@RestController
@RequestMapping( "questionLib")
public class QuestionLibController extends BaseInfoProperties {

    @Resource
    private QuestionLibService questionLibService;

    @PostMapping("/createOrUpdate")
    public GraceJSONResult createOrUpdateInterview(@RequestBody QuestionLibBO questionLibBO) {
        questionLibService.createOrUpdateInterviewer(questionLibBO);
        return GraceJSONResult.ok();
    }

    /**
     * 查询面试分页题库
     * @param aiName
     * @param question
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public GraceJSONResult queryList(@RequestParam String aiName,
                                    @RequestParam String question,
                                    @RequestParam(defaultValue = "1",name = "page") Integer page,
                                    @RequestParam(defaultValue = "10",name = "pageSize") Integer pageSize) {
        PagedGridResult pagedGridResult = questionLibService.queryList(aiName, question, page, pageSize);

        return GraceJSONResult.ok(pagedGridResult);
    }


    /**
     * @Description: 设置某个面试题显示（启用）
     * @param questionLibId
     * @return GraceJSONResult
     */
    @PostMapping("/show")
    public GraceJSONResult show(@RequestParam String questionLibId) {
        if (StringUtils.isBlank(questionLibId)){
            return GraceJSONResult.error();
        }
        questionLibService.setDisplayOrNot(questionLibId, YesOrNo.YES.type);
        return GraceJSONResult.ok();
    }

    /**
     * @Description: 设置某个面试题隐藏（禁用）
     * @param questionLibId
     * @return GraceJSONResult
     */
    @PostMapping("/hide")
    public GraceJSONResult hide(@RequestParam String questionLibId) {
        if (StringUtils.isBlank(questionLibId)){
            return GraceJSONResult.error();
        }
        questionLibService.setDisplayOrNot(questionLibId, YesOrNo.NO.type);
        return GraceJSONResult.ok();
    }

    /**
     * @Description: 删除指定的面试题
     * @Author 风间影月
     * @param questionLibId
     * @return GraceJSONResult
     */
    @PostMapping("/delete")
    public GraceJSONResult delete(@RequestParam String questionLibId) {
        if (StringUtils.isBlank(questionLibId)){
            return GraceJSONResult.error();
        }
        questionLibService.delete(questionLibId);
        return GraceJSONResult.ok();
    }

    /**
     * @Description: 准备面试题，随机获得一定数量的面试题返回给前端
     * @param candidateId
     * @return GraceJSONResult
     */
    @GetMapping("prepareQuestion")
    public GraceJSONResult prepareQuestion(@RequestParam String candidateId) {

        // 判断应聘者候选人是否在会话中，限制接口被恶意调用
        String candidateInfo = redis.get(REDIS_USER_INFO + ":" + candidateId);
        String userToken = redis.get(REDIS_USER_TOKEN + ":" + candidateId);
        if (StringUtils.isBlank(candidateInfo) || StringUtils.isBlank(userToken)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_INFO_NOT_EXIST_ERROR);
        }

        List<InitQuestionsVO> result = questionLibService.getRandomQuestions(candidateId, 2);
        return GraceJSONResult.ok(result);
    }


}
